package com.yeu.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.yeu.model.Sms;
import com.yeu.util.ConnectionPool;
import com.yeu.util.SMSTool;

public class SmsAction extends ActionSupport implements ModelDriven<Sms>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Sms sms=new Sms();
	private Map<String, Boolean> boolResult;
	
	public Sms getSms() {
		return sms;
	}
	
	public void setSms(Sms sms) {
		this.sms = sms;
	}
	
	public Map<String, Boolean> getBoolResult() {
		return boolResult;
	}

	@Override
	public Sms getModel() {
		return this.sms;
	}

	@Override
	public String execute() throws Exception {
		boolResult=new HashMap<String, Boolean>();
		Boolean flag=false;
		String pre=String.format("尊敬的%s老师，我是仰恩大学学院110指挥平台的工作人员。", sms.getName());
		if(SMSTool.getOpenState()==SMSTool.STATE.OPEN && SMSTool.getSendState()==SMSTool.STATE.NORMAL){
			if(SMSTool.sendMessage(sms.getAddr(), pre+sms.getText())){
				Date date=new Date();
				DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String newTime=format.format(date);
				String  sql="insert into MESSAGE (MESSAGEID,MESSAGEDATE,MESSAGEPHONE,MESSAGECONTENT,MESSAGETYPE,MESSAGENAME) values(INCREASE_SEQUENCE.nextval,?,?,?,?,?)";
				flag=ConnectionPool.executeUpdate(sql,newTime,sms.getAddr(),sms.getText(),"S",sms.getName()).get("RESULT");
			}
		}
		
		boolResult.put("RESULT", flag);
		return "boolResult";
	}
}
